﻿using GPSTrackingWebsiteManager.ClassManager;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace GPSTrackingWebsiteManager.Manager.Pages
{
    public partial class Account : ClassManager.SessionManagerPage
    {
        private void Invoke_User(int CompanyID, string Username)
        {
            SessionManager objSessionManager = (SessionManager)HttpContext.Current.Session["SessionManager"];
            if (objSessionManager.IsMaster)
            {
                GPSTrackingDBV2.Entities.Masters masterEntity = GPSTrackingDBV2.Data.DataRepository.MastersProvider.GetByMasterUsername(objSessionManager.MasterUsername);

                txtUsername.Text = masterEntity.MasterUsername;

                txtName.Text = masterEntity.Name;
                txtFirstName.Text = masterEntity.FirstName;
                txtLastName.Text = masterEntity.LastName;
                if (masterEntity.Phone != null) txtTelephone.Text = masterEntity.Phone.ToString();
                if (masterEntity.Mobile != null) txtMobile.Text = masterEntity.Mobile.ToString();
                txtEmail.Text = masterEntity.Email;

                ViewState["Password"] = masterEntity.Password;
                ViewState["CreationDate"] = masterEntity.CreationDate;
                ViewState["IsActive"] = masterEntity.IsActive;

            }

            if (objSessionManager.IsAdmin)
            {
                GPSTrackingDBV2.Entities.Administrators adminEntity = GPSTrackingDBV2.Data.DataRepository.AdministratorsProvider.GetByAdminUsernameCompanyId(objSessionManager.AdminUsername, objSessionManager.CompanyID);

                txtUsername.Text = adminEntity.AdminUsername;

                txtName.Text = adminEntity.Name;
                txtFirstName.Text = adminEntity.FirstName;
                txtLastName.Text = adminEntity.LastName;
                if (adminEntity.Phone != null) txtTelephone.Text = adminEntity.Phone.ToString();
                if (adminEntity.Mobile != null) txtMobile.Text = adminEntity.Mobile.ToString();
                txtEmail.Text = adminEntity.Email;

                ViewState["Password"] = adminEntity.Password;
                ViewState["CreationDate"] = adminEntity.CreationDate;
                ViewState["IsActive"] = adminEntity.IsActive;

            }          

        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                SessionManager objSessionManager = (SessionManager)HttpContext.Current.Session["SessionManager"];
                if (objSessionManager.IsMaster) Invoke_User(objSessionManager.CompanyID, objSessionManager.MasterUsername);
                if (objSessionManager.IsAdmin) Invoke_User(objSessionManager.CompanyID, objSessionManager.AdminUsername);
            }
        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
            try
            {
                SessionManager objSessionManager = (SessionManager)HttpContext.Current.Session["SessionManager"];
                if (objSessionManager.IsMaster)
                {
                    GPSTrackingDBV2.Entities.Masters masterEntity = new GPSTrackingDBV2.Entities.Masters();

                    masterEntity.MasterUsername = txtUsername.Text;
                    masterEntity.OriginalMasterUsername = masterEntity.MasterUsername;
                    masterEntity.Password = Convert.ToString(ViewState["Password"]);
                    masterEntity.Name = txtName.Text;
                    masterEntity.FirstName = txtFirstName.Text;
                    masterEntity.LastName = txtLastName.Text;
                    masterEntity.CountryId = 0;
                    masterEntity.StateId = 0;
                    masterEntity.CityId = 0;
                    if (!string.IsNullOrEmpty(txtTelephone.Text)) masterEntity.Phone = txtTelephone.Text;
                    if (!string.IsNullOrEmpty(txtMobile.Text)) masterEntity.Mobile = txtMobile.Text;
                    masterEntity.Email = txtEmail.Text;

                    masterEntity.CreationDate = Convert.ToDateTime(ViewState["CreationDate"]);
                    masterEntity.IsActive = Convert.ToBoolean(ViewState["IsActive"]);

                    GPSTrackingDBV2.Data.DataRepository.MastersProvider.Update(masterEntity);
                }
                else
                {
                    GPSTrackingDBV2.Entities.Administrators adminEntity = new GPSTrackingDBV2.Entities.Administrators();

                    adminEntity.AdminUsername = txtUsername.Text;
                    adminEntity.OriginalAdminUsername = adminEntity.AdminUsername;
                    adminEntity.Password = Convert.ToString(ViewState["Password"]);
                    adminEntity.Name = txtName.Text;
                    adminEntity.FirstName = txtFirstName.Text;
                    adminEntity.LastName = txtLastName.Text;
                    adminEntity.CountryId = 0;
                    adminEntity.StateId = 0;
                    adminEntity.CityId = 0;
                    if (!string.IsNullOrEmpty(txtTelephone.Text)) adminEntity.Phone = txtTelephone.Text;
                    if (!string.IsNullOrEmpty(txtMobile.Text)) adminEntity.Mobile = txtMobile.Text;
                    adminEntity.Email = txtEmail.Text;

                    adminEntity.CreationDate = Convert.ToDateTime(ViewState["CreationDate"]);
                    adminEntity.IsActive = Convert.ToBoolean(ViewState["IsActive"]);

                    GPSTrackingDBV2.Data.DataRepository.AdministratorsProvider.Update(adminEntity);
                }

                ShowMessage("Información Actualizada");

            }
            catch (Exception ex)
            {
                showErrorMessage(ex.Message);
            }
        }

        public void showErrorMessage(string msg)
        {
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "objWindowsMessageManager.show_Error", "objWindowsMessageManager.show_Error('" + msg + "','Cambio de contraseña...');", true);
        }

        public void ShowMessage(string msg)
        {
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "objWindowsMessageManager.show_Info", "objWindowsMessageManager.show_Info('" + msg + "','Recuperación de contraseña');", true);
        }

    }
}